﻿Imports System.Data


Imports com.auo.flower.remote
Partial Class WebForm_AssetMaintain_Component_AssetFlowER
    Inherits BaseUserControl
    Protected FormKind As String = "AUO.FORM.87"
    Public Property myDT() As Datatable
        Get
            If ViewState("myDT") Is Nothing Then
                Dim dt As DataTable
                dt = New DataTable()
                dt.Columns.Add("asset_serialNo", System.Type.GetType("System.String"))
                dt.Columns.Add("Def_Name", System.Type.GetType("System.String"))
                dt.Columns.Add("ModiType", System.Type.GetType("System.String"))
                dt.Columns.Add("asset_owner", System.Type.GetType("System.String"))
                dt.Columns.Add("asset_ownerID", System.Type.GetType("System.String"))
                dt.Columns.Add("User2", System.Type.GetType("System.String"))
                dt.Columns.Add("NewOwner", System.Type.GetType("System.String"))
                dt.Columns.Add("NewUser2", System.Type.GetType("System.String"))
                dt.Columns.Add("OtherUser", System.Type.GetType("System.String"))

                ViewState("myDT") = dt
                Return ViewState("myDT")
            Else
                Return ViewState("myDT")
            End If
        End Get
        Set(ByVal value As DataTable)
            gvMain.DataSource = value
            gvMain.DataBind()
            ViewState("myDT") = value
        End Set
    End Property

    Public Event onShowFinish(ByVal source As Object, ByVal e As EventArgs)
    Public Event onShow(ByVal source As Object, ByVal e As EventArgs)

    Public Event onShowPreviouse(ByVal source As Object, ByVal e As EventArgs)
    Public Event DeleteData(ByVal source As Object, ByVal e As EventArgs)
    Public Event DeleteAllData(ByVal source As Object, ByVal e As EventArgs)


    Protected Sub btnOK_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnOK.Click

        ''test  =================
        'ViewState.Remove("myDT") '清除暫存資料
        'RaiseEvent DeleteAllData(Nothing, New EventArgs)
        'Exit Sub

        '============================================


        ''要先存檔後再申請87form ,105Form''除帳先不做.等87Form 包含除帳的資料為止
        Try

            Dim formNo As Integer = 0
            '  Dim Form87 As AssetManage.Manager.From87Remote = New AssetManage.Manager.From87Remote(Me.Page)
            Dim dao As AssetManage.DAO.AssetDAO = New AssetManage.DAO.AssetDAO(dbName)
            Dim uBBL As OAMS.BBL.UsersBBL = New OAMS.BBL.UsersBBL()
            Dim uDao As OAMS.DAO.UsersDAO = New OAMS.DAO.UsersDAO(dbName)
            Dim dr1, dr2 As DataRow
            Dim result As Boolean
            Dim approve1 As String = ""
            Dim approve2 As String = ""
            Dim approve3 As String = ""
            Dim approveoa As String = ""
            Dim approvescrap As String = ""
            dr1 = uDao.GetEmpData(ActualUID).Rows(0)
            Try
                dr2 = uDao.GetAUOFORM87_CODELIST(dr1("Factory_ID").ToString(), "2").Rows(0)
                approve1 = dr2("approver1").ToString
                approve2 = dr2("approver2").ToString
                approve3 = dr2("approver3").ToString
                approveoa = dr2("approveroa").ToString
            Catch ex As Exception
                ShowMessageForAjax(btnClose, "Form 87 FlowER not define  " + dr1("Factory_ID").ToString() + "")
                Return
            End Try

            Try
                dr2 = uDao.GetAUOFORM87_CODELIST(dr1("Factory_ID").ToString(), "1").Rows(0)
                approvescrap = dr2("approverScrap").ToString
            Catch ex As Exception
                ShowMessageForAjax(btnClose, "Form 87 FlowER not define  " + dr1("Factory_ID").ToString() + "")
                Return
            End Try
            ''找出所有的NewOwner========================================
            Dim newUser As String = ""
            For Each dr As DataRow In myDT.Rows
                If dr("ModiType") = "1" Or dr("ModiType") = "2" Then
                    If newUser.IndexOf(dr("NewOwner").ToString.Trim) = -1 Then
                        newUser += dr("NewOwner").ToString.Trim & "@"
                    End If
                End If
            Next

            newUser = newUser.TrimEnd("@")
            Dim objhelper As IRemotingFlowHelper = DirectCast(RemotingHelperCreator.CreateInstance(GetType(IRemotingFlowHelper)), IRemotingFlowHelper)

            '//=====================================
            For Each dr As DataRow In myDT.Rows
                If dr("ModiType") = "1" Or dr("ModiType") = "2" Then
                    ''新增到Form87的table中asset中 
                    'formNo = Form87.createform(ActualUID)er
                    formNo = objhelper.CreateNewForm(FormKind, ActualUID)

                    ''formNo = Integer.Parse(Now.ToString("HHmmssff"))

                    dao.InsertForm87DataHead(formNo, ActualUID, dr1("Dept_ID").ToString(), dr1("Ext_No").ToString, "N/A", "OA資產移轉說明")

                    ' Form87.AddFlag(formNo, "", Now.ToString("yyyy/MM/dd HH:mm:ss"), ActualUID, ActualUserName, "", "", "", dr("ModiType").ToString(), "6", "")
                    Dim objParas(7) As IParameter

                    Dim ownerid As String = dr("asset_ownerID").ToString()

                    objParas(0) = New para("OWNER_BEFORE", ownerid)
                    objParas(1) = New para("OWNER_LATER", newUser)
                    If dr("ModiType") = "1" Then
                        objParas(2) = New para("FLAG", "Distributed")
                    Else
                        objParas(2) = New para("FLAG", "Storing")
                    End If
                    objParas(3) = New para("APPLY_ORG", dr1("company").ToString)
                    objParas(4) = New para("APPROVER1", approve1)
                    objParas(5) = New para("APPROVER2", approve2)
                    objParas(6) = New para("APPROVER3", approve3)
                    objParas(7) = New para("APPROVEROA", approvescrap)
                    result = objhelper.AddFlag(FormKind, formNo, objParas)
                    'Form87.active(formNo)
                    ''送出FlowER
                    'Form87.send(formNo, "Insert")
                    result = objhelper.SendForm(FormKind, formNo, ActualUID)

                    result = objhelper.ActiveForm(FormKind, formNo)
                    Exit For
                End If
            Next

            If formNo <> 0 Then ''87Form有資料
                ''新增到Form87的table中asset中
                For Each dr As DataRow In myDT.Rows
                    If dr("ModiType") = "1" Or dr("ModiType") = "2" Then
                        dao.InsertForm87DataDetail(dr("ModiType").ToString, formNo, dr("asset_serialNo").ToString, dr("asset_ownerID").ToString, dr("NewOwner").ToString, dr("NewUser2").ToString, dr("OtherUser").ToString, ActualUID, approve1, approve2, approve3, approvescrap)
                    End If
                Next
            End If

            ''除帳資料另外申請單
            formNo = 0
            ''找出所有的NewOwner========================================
            newUser = ""
            For Each dr As DataRow In myDT.Rows
                If dr("ModiType") = "3" Then
                    If newUser.IndexOf(dr("NewOwner").ToString.Trim) = -1 Then
                        newUser += dr("NewOwner").ToString.Trim & "@"
                    End If
                End If
            Next

            newUser = newUser.TrimEnd("@")
            ''=================================================
            For Each dr As DataRow In myDT.Rows
                If dr("ModiType") = "3" Then

                    formNo = objhelper.CreateNewForm(FormKind, ActualUID)


                    dr1 = uDao.GetEmpData(ActualUID).Rows(0)
                    dao.InsertForm87DataHead(formNo, ActualUID, dr1("Dept_ID").ToString(), dr1("Ext_No").ToString, "N/A", "OA除帳說明")

                    Dim objParas As IParameter() = New IParameter(7) {}
                    objParas(0) = New para("OWNER_BEFORE", dr("asset_ownerID").ToString)
                    objParas(1) = New para("OWNER_LATER", newUser)

                    objParas(2) = New para("FLAG", "Scrapping")
                    objParas(3) = New para("APPLY_ORG", dr1("company").ToString)
                    objParas(4) = New para("APPROVER1", approve1)
                    objParas(5) = New para("APPROVER2", approve2)
                    objParas(6) = New para("APPROVER3", approve3)
                    objParas(7) = New para("APPROVEROA", approveoa)
                    objhelper.AddFlag(FormKind, formNo, objParas)

                    result = objhelper.SendForm(FormKind, formNo, ActualUID)
                    result = objhelper.ActiveForm(FormKind, formNo)
                    Exit For
                End If
            Next

            If formNo <> 0 Then ''87Form有資料
                ''新增到Form87的table中asset中
                For Each dr As DataRow In myDT.Rows
                    If dr("ModiType") = "3" Then
                        ' dao.InsertForm87DataDetail(dr("ModiType").ToString, formNo, dr("asset_serialNo").ToString, dr("asset_ownerID").ToString, approveoa, approveoa, dr("OtherUser").ToString, ActualUID, approve1, approve2, approve3, approveoa)
                        dao.InsertForm87DataDetail(dr("ModiType").ToString, formNo, dr("asset_serialNo").ToString, dr("asset_ownerID").ToString, dr("NewOwner").ToString, dr("NewOwner").ToString, "", ActualUID, approve1, approve2, approve3, approveoa)
                    End If
                Next
            End If

            ViewState.Remove("myDT") '清除暫存資料
            RaiseEvent DeleteAllData(Nothing, New EventArgs)
            ShowMessageForAjax(btnOK, GetLocalResourceObject("Finish"))
            RaiseEvent onShowFinish(sender, New EventArgs())
        Catch ex As Exception

            ShowMessageForAjax(btnOK, ex.Message)
        End Try

    End Sub

    

    Protected Sub btnPreviouse_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnPrevious.Click


        RaiseEvent onShowPreviouse(gvMain, New EventArgs)
    End Sub

  
   


  
    Protected Sub gvMain_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles gvMain.RowDeleting
        DelIndex = e.RowIndex
        Dim btn As Object = CType(gvMain.Rows(e.RowIndex).FindControl("btnDelete"), LinkButton)
        Dim asset_serialNo As String = CType(gvMain.Rows(e.RowIndex).FindControl("btnDelete"), LinkButton).CommandArgument.ToString()
        Dim drs() As DataRow = myDT.Select("asset_serialNo='" + asset_serialNo + "'")
        For Each dr As DataRow In drs
            dr.Delete()
        Next


        gvMain.DataSource = myDT
        gvMain.DataBind()
        RaiseEvent DeleteData(btn, New EventArgs)
        If gvMain.Rows.Count > 0 Then
            If DelIndex < gvMain.Rows.Count Then
                RunClientScriptForAjax(Me.Page, "setTimeout(""$get('" & gvMain.Rows(DelIndex).FindControl("btnDelete").ClientID & "').focus(); "", 10);")
            Else
                RunClientScriptForAjax(Me.Page, "setTimeout(""$get('" & gvMain.Rows(gvMain.Rows.Count - 1).FindControl("btnDelete").ClientID & "').focus(); "", 10);")
            End If
        End If

    End Sub

    Public Property DelIndex() As Integer
        Get
            Return Integer.Parse(ViewState("_DelIndex").ToString)
        End Get
        Set(ByVal value As Integer)
            ViewState("_DelIndex") = value
        End Set
    End Property

End Class
